package com.norteksoft.erm.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * 功能资源表
 * 
 * @hibernate.class table="ERM_RESOURCE_CONFIG"
 */
public class Resource extends BaseObject  {

	private static final long serialVersionUID = 677221780128196531L;

	public static final String PATH_SEPARATOR = "-";

	public static final String TYPE_PACKAGE = "pkg";// 模块

	public static final String TYPE_FUNC = "func";// 功能点

	public static final String DIS_PROTECT = "false", EN_PROTECT = "true";

	private Long rsID = new Long(-1);// OID

	// 层次属性 ====================================
	private WebModule webModule;// 资源所属子系统

	private Resource parentResource;// 资源只有两层关系（包/资源）

	private List childResources = new ArrayList();// 子资源集

	// 资源属性 ====================================

	private String resourceName;// 资源名称

	private String resourceID;// 资源ID funcation_xxxx

	private String actionPath;// 资源路径

	private Integer displayOrder;// 显示排序

	private String memo;// 备注

	private String type;// 资源类型（模块/功能点）

	private String isOpen;// 保护状态？true/false

	private String requiresHttps;// 是否强制使用https来访问

	private String fullPath;// 完整路径
	
	private String isNav; //是否在导航显示

	// 授权相关属性 ====================================

	private Set grantedPermissions = new HashSet();// 功能资源授予的权限集合

	private Set grantedDelegates = new HashSet();// 功能资源授予的委托集合

	// 日志相关属性 ====================================

	private FuncLogDefinition funcLogDefinition;// 日志设置

	// 遗留或未知属性 ====================================
	private String logable;// 是否应用于记录日志？true/false

	private String state;// 资源状态

	private String resourceState;

	private String hasChild;// 是否拥有下级功能 true/false

	public int hashCode() {
		return new HashCodeBuilder().append(rsID).append(resourceName).append(
				resourceID).append(actionPath).append(displayOrder)
				.append(memo).append(type).append(isOpen).append(logable)
				.append(requiresHttps).append(fullPath).append(state).append(
						resourceState).toHashCode();
	}

	public Resource() {
	}

	/**
	 * @hibernate.id generator-class="sequence" column="RSID" unsaved-value="-1"
	 *               type="long"
	 * @hibernate.generator-param name="sequence" value="ERM_SEQUENCE"
	 */
	public Long getRsID() {
		return rsID;
	}

	public void setRsID(Long rsID) {
		this.rsID = rsID;
	}

	/**
	 * @hibernate.property column="RESOURCEID" length="255" type="string"
	 *                     not-null="true"
	 * @return String
	 */
	public String getResourceID() {
		return resourceID;
	}

	public void setResourceID(String resourceID) {
		this.resourceID = resourceID;
	}

	/**
	 * @hibernate.property column="RESOURCENAME" length="255" type="string"
	 * @return String
	 */
	public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}

	/**
	 * @hibernate.property column="ACTIONPATH" length="255" type="string"
	 * @return String
	 */
	public String getActionPath() {
		return actionPath;
	}

	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}

	/**
	 * @hibernate.many-to-one name="webModule" column="WEBMODULEID"
	 *                        class="com.norteksoft.erm.model.WebModule"
	 */
	public WebModule getWebModule() {
		return webModule;
	}

	public void setWebModule(WebModule webModule) {
		this.webModule = webModule;
	}

	/**
	 * @hibernate.property column="MEMO" length="1000" type="string"
	 * @return String
	 */
	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	/**
	 * @hibernate.property column="ISOPEN" length="15" type="string"
	 * @return String
	 */

	public String getIsOpen() {
		return isOpen;
	}

	public void setIsOpen(String isOpen) {
		this.isOpen = isOpen;
	}

	/**
	 * @hibernate.property column="STATE" length="255" type="string"
	 * @return String
	 */
	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	/**
	 * @hibernate.property column="REQUIRESHTTPS" length="12" type="string"
	 * @return String
	 */

	public String getRequiresHttps() {
		return requiresHttps;
	}

	public void setRequiresHttps(String requiresHttps) {
		this.requiresHttps = requiresHttps;
	}

	/**
	 * @hibernate.property column="RESOURCESTATE" length="12" type="string"
	 * @return String
	 */
	public String getResourceState() {
		return resourceState;
	}

	public void setResourceState(String resourceState) {
		this.resourceState = resourceState;
	}

	/**
	 * @hibernate.many-to-one name="parentResource" column="PARENTID"
	 *                        cascade="none" outer-join="auto"
	 */
	public Resource getParentResource() {
		return parentResource;
	}

	public void setParentResource(Resource parentResource) {
		this.parentResource = parentResource;
	}

	/**
	 * @hibernate.bag name="childResources" cascade="all" lazy="true"
	 *                inverse="true" order-by="resourceName asc"
	 * @hibernate.key column="PARENTID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.Resource"
	 * @return List
	 */
	public List getChildResources() {
		return childResources;
	}

	public void setChildResources(List childResources) {
		this.childResources = childResources;
	}

	/**
	 * @hibernate.set lazy="true" inverse="true" cascade="all-delete-orphan"
	 *                order-by="authorityType asc"
	 * @hibernate.key column="RSID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.FuncPermission"
	 * @return Set
	 */
	public Set getGrantedPermissions() {
		return grantedPermissions;
	}

	public void setGrantedPermissions(Set grantedPermissions) {
		this.grantedPermissions = grantedPermissions;
	}

	/**
	 * @hibernate.property column="TYPE" length="8" type="string"
	 * @return String
	 */
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	/**
	 * 是否是已保护的功能
	 * 
	 * @return
	 */
	public boolean isProtectedFunc() {
		return (TYPE_FUNC.equals(this.type) || StringUtils.isBlank(this.type))
				&& "true".equals(this.isOpen);
	}

	/**
	 * @hibernate.component class="com.norteksoft.erm.model.FuncLogDefinition"
	 */
	public FuncLogDefinition getFuncLogDefinition() {
		return funcLogDefinition;
	}

	public void setFuncLogDefinition(FuncLogDefinition funcLogDefinition) {
		this.funcLogDefinition = funcLogDefinition;
	}

	/**
	 * @hibernate.property column="DISPLAYORDER" type="integer"
	 * @return Integer
	 */
	public Integer getDisplayOrder() {
		return displayOrder;
	}

	public void setDisplayOrder(Integer displayOrder) {
		this.displayOrder = displayOrder;
	}

	/**
	 * @hibernate.property column="LOGABLE" length="6" type="string"
	 * @return String
	 */
	public String getLogable() {
		return logable;
	}

	public void setLogable(String logable) {
		this.logable = logable;
	}

	/**
	 * @hibernate.set lazy="true" inverse="true" cascade="all-delete-orphan"
	 * @hibernate.key column="RSID"
	 * @hibernate.one-to-many class="com.norteksoft.erm.model.FuncDelegate"
	 * @return Set
	 */
	public Set getGrantedDelegates() {
		return grantedDelegates;
	}

	public void setGrantedDelegates(Set grantedDelegates) {
		this.grantedDelegates = grantedDelegates;
	}

	public String getParentID() {
		if (this.getParentResource() != null
				&& !"".equals(this.getParentResource())) {
			return this.getParentResource().getRsID().toString();
		} else {
			return "";
		}
	}

	public String getNodeID() {
		return this.getRsID().toString();
	}

	public String getNodeName() {
		return (this.getResourceID() + "（" + this.getResourceName() + "）");
	}

	/**
	 * @hibernate.property column="FULLPATH" length="60" type="string"
	 * @return String
	 */
	public String getFullPath() {
		return fullPath;
	}

	public void setFullPath(String fullPath) {
		this.fullPath = fullPath;
	}

	/**
	 * @hibernate.property column="HASCHILD" length="10" type="string"
	 * @return String
	 */
	public String getHasChild() {
		return hasChild;
	}

	public void setHasChild(String hasChild) {
		this.hasChild = hasChild;
	}

	public String toString() {
		return "R" + this.rsID.toString();

	}
	
	/**
	 * @hibernate.property column="ISNAV" length="50" type="string"
	 * @return String
	 */
	public String getIsNav() {
		return isNav;
	}

	public void setIsNav(String isNav) {
		this.isNav = isNav;
	}
}
